package BTree;

public class BTree {
 static  class TreeNode{
     public char val;
      public TreeNode left;
      public TreeNode right;

     public TreeNode(char val) {
         this.val = val;
     }
 }

public TreeNode create(){
     TreeNode A=new TreeNode('A');
     TreeNode B=new TreeNode('B');
     TreeNode C=new TreeNode('C');
     TreeNode D=new TreeNode('D');
     TreeNode E=new TreeNode('E');
     TreeNode F=new TreeNode('F');
     TreeNode G=new TreeNode('G');
     TreeNode N=new TreeNode('N');

     A.left=B;
     A.right=C;
     B.left=D;
     B.right=E;
     C.left=F;
     C.right=G;
D.left=N;
     return A;
}


//前序遍历
public void preOrder(TreeNode root){
     if(root==null){
         return ;
     }
    System.out.print(root.val+" ");
     preOrder(root.left);
     preOrder(root.right);
}
//中序遍历
public void inOrder(TreeNode root){
     if(root==null){
         return ;
     }
     inOrder(root.left);
    System.out.print(root.val+" ");
     inOrder(root.right);
}
//后序遍历
public void postOrder(TreeNode root){
     if(root==null){
         return;
     }
     postOrder(root.left);
     postOrder(root.right);
    System.out.print(root.val+" ");
}

//获取二叉树节点的个数
    public int size=0;
    public void getBtreesize(TreeNode root){
        if(root==null){
            return;
        }
        size++;
        getBtreesize(root.left);
        getBtreesize(root.right);
    }

    public int getSize(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        return getSize(root.left)+getSize(root.right)+1;
    }

    //获取第k层的节点个数
    public int getKsize(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        if(k==1){
            return 1;
        }
        return getKsize(root.left,k-1)+getKsize(root.right,k-1);
    }

//获取二叉树的高度

    public int getHigh(TreeNode root){
        if(root==null){
            return 0;
        }
        return Math.max(getHigh(root.left),getHigh(root.right))+1;
    }




}
